import datetime
import random

import os

from django.http import JsonResponse
from django.shortcuts import render

from cart.models import ShoppingCart
from order.models import OrderInfo, OrderGoods


def place_order(request):
    if request.method == 'GET':
        user_id = request.session.get('user_id')
        carts = ShoppingCart.objects.filter(user_id=user_id,
                                            is_select=1).all()
        for cart in carts:
            cart.total_price = int(cart.nums) * int(cart.goods.shop_price)
        return render(request,'place_order.html',{'carts': carts})

def order(request):
    if request.method == 'POST':
        # 1.从购物车表中取出当前登录系统用户且is_select为1的商品信息
        user_id = request.session.get('user_id')
        carts = ShoppingCart.objects.filter(user_id=user_id,
                                    is_select=1).all()
        #计算总金额
        order_mount = 0
        for cart in carts:
            order_mount += int(cart.nums) * int(cart.goods.shop_price)
        # 2、创建订单
        today = datetime.date.today()
        message = 'abcdefghijklmnopqrstuvwxyz1234567890'
        sn = ''
        for i in range(10):
            sn += random.choice(message)
        today_sn = str(today) + sn
        order = OrderInfo.objects.create(user_id=user_id,
                                         order_sn=today_sn,
                                         order_mount=order_mount)
        # 3、创建订单详情信息
        for cart in carts:
            OrderGoods.objects.create(order=order,
                                      goods=cart.goods,
                                      goods_nums=cart.nums)
        # 4、删除购物车中已经下单的商品信息
        carts.delete()
        return JsonResponse({'code':200,'msg':'请求成功1111111111111'})